package com.egreen.pointsmall.controller;


import com.egreen.common.States;
import com.egreen.common.pojo.entity.pointsmall.PointsUser;
import com.egreen.pointsmall.exception.PointsmallException;
import com.egreen.pointsmall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;


@Controller
public class RedirectController {
    @Autowired
    private ICartService cartService;
    @GetMapping("/{url}")
    public ModelAndView redirect(@PathVariable("url") String url, HttpSession session) {
        //判断是否登录
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(url);
        PointsUser user = (PointsUser) session.getAttribute("user");
        if (user == null) {
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            //查询购物车中的数量
            modelAndView.addObject("cartList", cartService.getCartVoList(user.getId()));
        }
        return modelAndView;
    }
    @GetMapping("/")
    public String main() {
        return "redirect:/productCategory/main";
    }
    @GetMapping("favicon.ico")
    @ResponseBody
    void returnNoFavicon() {
    }


}
